package com.qingcheng.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.controller.util.WebUtil;
import com.qingcheng.pojo.system.LoginLog;
import com.qingcheng.service.system.LoginLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @Author:boy
 * @Date:2019/7/3
 * @description:
 * @ModifiedBy:
 */
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {
    @Reference
    private LoginLogService loginLogService;
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        Authentication authentication) throws IOException, ServletException {
        Logger logger= LoggerFactory.getLogger(AuthenticationSuccessHandlerImpl.class);
        logger.info("-->login success 记录登录日志....");
        String name = authentication.getName();
        String remoteAddr = httpServletRequest.getRemoteAddr();
        String agent = httpServletRequest.getHeader("User-Agent");
        LoginLog loginLog=new LoginLog();
        loginLog.setLoginName(name);
        loginLog.setBrowserName(WebUtil.getBrowserName(agent));
        loginLog.setIp(remoteAddr);
        loginLog.setLocation(WebUtil.getCityByIP(remoteAddr));
        loginLog.setLoginTime(new Date());
        loginLogService.add(loginLog);
        //跳转到首页
        httpServletRequest.getRequestDispatcher("/main.html").forward(httpServletRequest,httpServletResponse);


    }
}
